# 给定一个字符串，请你找出其中不含有重复字符的 最长子串 的长度。
# 输入: "abcabcbb"
# 输出: 3
# 解释: 因为无重复字符的最长子串是 "abc"，所以其长度为 3。
# 输入: "bbbbb"
# 输出: 1
# 解释: 因为无重复字符的最长子串是 "b"，所以其长度为 1。
# 输入: "pwwkew"
# 输出: 3
# 解释: 因为无重复字符的最长子串是 "wke"，所以其长度为 3。
# 请注意，你的答案必须是 子串 的长度，"pwke" 是一个子序列，不是子串。


# def lengthOfLongestSubstring(s):
#     str_list = list(s)
#     len_str = len(str_list)
#     if len_str == 0:
#         return 0
#     result = 1
#     for i in enumerate(str_list):
#         son_list = [i[1]]
#         count = 1
#         while True:
#             if i[0] + count >= len_str:
#                 break
#             num = str_list[i[0] + count]
#             if num not in son_list:
#                 son_list.append(num)
#                 count += 1
#             else:
#                 break
#         len_son_list = len(son_list)
#         if len_son_list > result:
#             result = len_son_list
#     return result


def lengthOfLongestSubstring(s):
    length = len(s)
    i, j, res = 0, 1, 0
    mid_set = set(s[i:j])
    while i + res < length:
        while j <= length:
            if j == length:
                res = max(j - i, res)
                return res
            elif s[j] not in mid_set:
                mid_set.add(s[j])
                j += 1
            else:
                break
        res = max(j - i, res)
        i += (s[i:j].find(s[j]) + 1)
        mid_set = set(s[i:j])
    return res


class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        """这个最快"""
        length = len(s)
        i, j = 0, 1
        res = 0
        while i + res < length:
            while j <= length:
                if j == length:
                    res = max(j - i, res)
                    return res
                elif s[j] not in s[i:j]:
                    j += 1
                else:
                    break
            res = max(j - i, res)
            i += (s[i:j].find(s[j]) + 1)
        return res


print(lengthOfLongestSubstring(""))
